package com.jyw.business.ins.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jyw.business.ast.domain.dto.web.InsRectifyAstPageDto;
import com.jyw.business.ast.domain.dto.wechat.WechatInsRectifyAstPageDto;
import com.jyw.business.ast.domain.vo.web.InsRectifyAstPageVo;
import com.jyw.business.ins.domain.InsTask;
import com.jyw.business.ins.domain.InsTaskRectify;
import com.jyw.business.ins.domain.dto.web.RectifyDto;
import com.jyw.business.ins.domain.dto.web.RectifyUnitPageDto;
import com.jyw.business.ins.domain.dto.web.TransmitDto;
import com.jyw.business.ins.domain.dto.wechat.WechatRectifyUnitPageDto;
import com.jyw.business.ins.domain.vo.web.*;
import com.jyw.business.ins.mapper.InsTaskRectifyMapper;
import com.jyw.business.ins.service.*;
import com.jyw.common.exception.ServiceException;
import com.jyw.common.exception.ValidationException;
import com.jyw.system.utils.ConversionUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 巡检任务整改Service业务层处理
 * 
 * @author system
 * @date 2023-10-07
 */
@Service
public class InsTaskRectifyServiceImpl extends ServiceImpl<InsTaskRectifyMapper,InsTaskRectify> implements IInsTaskRectifyService
{
    @Resource
    private InsTaskRectifyMapper insTaskRectifyMapper;

    @Resource
    private IInsTaskItemService taskItemService;

    @Resource
    private IInsTaskService taskService;

    @Resource
    private IInsPlanService planService;

    @Resource
    private IInsTransmitRecordService transmitRecordService;

    /**
     * 查询巡检任务整改
     * 
     * @param id 巡检任务整改主键
     * @return 巡检任务整改
     */
    @Override
    public InsTaskRectify selectInsTaskRectifyById(Long id)
    {
        return insTaskRectifyMapper.selectInsTaskRectifyById(id);
    }

    /**
     * 查询巡检任务整改列表
     * 
     * @param insTaskRectify 巡检任务整改
     * @return 巡检任务整改
     */
    @Override
    public List<InsTaskRectify> selectInsTaskRectifyList(InsTaskRectify insTaskRectify)
    {
        return insTaskRectifyMapper.selectInsTaskRectifyList(insTaskRectify);
    }

    /**
     * 新增巡检任务整改
     * 
     * @param insTaskRectify 巡检任务整改
     * @return 结果
     */
    @Override
    public int insertInsTaskRectify(InsTaskRectify insTaskRectify)
    {
        insTaskRectify.setCreateTime(LocalDateTime.now());
        return insTaskRectifyMapper.insertInsTaskRectify(insTaskRectify);
    }

    /**
     * 修改巡检任务整改
     * 
     * @param insTaskRectify 巡检任务整改
     * @return 结果
     */
    @Override
    public int updateInsTaskRectify(InsTaskRectify insTaskRectify)
    {
        return insTaskRectifyMapper.updateInsTaskRectify(insTaskRectify);
    }

    /**
     * 批量删除巡检任务整改
     * 
     * @param ids 需要删除的巡检任务整改主键
     * @return 结果
     */
    @Override
    public int deleteInsTaskRectifyByIds(Long[] ids)
    {
        return insTaskRectifyMapper.deleteInsTaskRectifyByIds(ids);
    }

    /**
     * 删除巡检任务整改信息
     * 
     * @param id 巡检任务整改主键
     * @return 结果
     */
    @Override
    public int deleteInsTaskRectifyById(Long id)
    {
        return insTaskRectifyMapper.deleteInsTaskRectifyById(id);
    }

    @Override
    public List<InsRectifyAstPageVo> rectifyAstList(InsRectifyAstPageDto dto) {
        List<InsRectifyAstPageVo> list = insTaskRectifyMapper.rectifyAstList(dto);
        list.forEach(data->{
            //超时未整改
            if(data.getIsRectify().equals(0)&&data.getRectifyEndDate().getTime()<System.currentTimeMillis()){
                data.setIsRectify(-1);
            }
        });
        return ConversionUtil.conversionList(list,InsRectifyAstPageVo.class);
    }

    @Override
    public List<RectifyUnitPageVo> rectifyUnitList(RectifyUnitPageDto dto) {
        List<RectifyUnitPageVo> list = insTaskRectifyMapper.rectifyUnitList(dto);
        list.forEach(data->{
            //超时未整改
            if(data.getIsRectify().equals(0)&&data.getRectifyEndDate().getTime()<System.currentTimeMillis()){
                data.setIsRectify(-1);
            }
        });
        return ConversionUtil.conversionList(list,RectifyUnitPageVo.class);
    }

    @Override
    public RectifyDetailVo getRectifyDetail(Long id) {
        InsTaskRectify rectify = getById(id);
        if(ObjectUtils.isEmpty(rectify)){
            throw new ServiceException("整改信息不存在");
        }
        List<TaskItemVo> taskItemVos = taskItemService.listByTaskId(rectify.getTaskId());
        RectifyDetailVo vo = new RectifyDetailVo(rectify, taskItemVos);
        vo = ConversionUtil.conversion(vo,RectifyDetailVo.class);

        InsTask task = taskService.getById(rectify.getId());
        InsObjInfo objInfo = planService.getObjInfo(task.getPlanId());
        vo.setObjInfo(objInfo);

        //转交任务
        List<InsTransmitRecordVO> recordList=transmitRecordService.queryListByRectifyId(id);
        if(CollectionUtil.isNotEmpty(recordList)){
            vo.setTransmitRecordList(recordList);
        }
        return vo;
    }

    @Override
    public void rectify(RectifyDto dto) {
        InsTaskRectify rectify = getById(dto.getId());
        if(ObjectUtils.isEmpty(rectify)){
            throw new ServiceException("整改信息不存在");
        }
        if(rectify.getIsRectify()){
            throw new ValidationException("已整改");
        }
        updateById(dto.buildRectify());
    }

    @Override
    public void transmit(TransmitDto dto) {
        InsTaskRectify rectify = getById(dto.getId());
        if(ObjectUtils.isEmpty(rectify)){
            throw new ServiceException("整改信息不存在");
        }
        if(rectify.getIsRectify()){
            throw new ValidationException("已整改,无需转交");
        }
        transmitRecordService.transmitRecord(dto.getId(),dto.getUserId());
        //修改整改责任人
        updateById(new InsTaskRectify(){{setId(dto.getId());setRectifyUserId(dto.getUserId());}});
    }

    @Override
    public List<InsRectifyAstPageVo> wxRectifyAstList(WechatInsRectifyAstPageDto dto) {
        List<InsRectifyAstPageVo> list = insTaskRectifyMapper.wxRectifyAstList(dto);
        list.forEach(data->{
            //超时未整改
            if(data.getIsRectify().equals(0)&&data.getRectifyTime().getTime()<System.currentTimeMillis()){
                data.setIsRectify(-1);
            }
        });
        return ConversionUtil.conversionList(list,InsRectifyAstPageVo.class);
    }

    @Override
    public List<RectifyUnitPageVo> wxRectifyUnitList(WechatRectifyUnitPageDto dto) {
        List<RectifyUnitPageVo> list = insTaskRectifyMapper.wxRectifyUnitList(dto);
        list.forEach(data->{
            //超时未整改
            if(data.getIsRectify().equals(0)&&data.getRectifyTime().getTime()<System.currentTimeMillis()){
                data.setIsRectify(-1);
            }
        });
        return ConversionUtil.conversionList(list,RectifyUnitPageVo.class);
    }
}
